package com.ice.project.datamining.gui;

import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

import org.jfree.data.general.DefaultValueDataset;
import org.jfree.ui.ApplicationFrame;

import com.ice.project.datamining.model.CreditProbability;

public class MainWindow extends ApplicationFrame {

	private JPanel mainPanel;
	private FormPanel formPanel;
	private ResultPanel resPanel;
	private CreditProbability probability;
	
	public MainWindow() {
		super("Data mining project");
		probability = new CreditProbability(this);
		initializeGUI();
		this.setResizable(false);
	}

	public CreditProbability getProbability() {
		return probability;
	}

	private void initializeGUI() {
		mainPanel = new JPanel();
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

		formPanel = new FormPanel(this);	
		resPanel = new ResultPanel(this);
		resPanel.setSize(300, 150);
		mainPanel.add(formPanel);
		mainPanel.add(resPanel);

		setContentPane(mainPanel);

		this.pack();
		setResizable(true);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setSize(800, 500);
		centerWindow();
	}

	private void centerWindow() {
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
	}
	
	public void refreshMeterPlot(double percent) {
		DefaultValueDataset ds = new DefaultValueDataset(percent);
		
		if (!resPanel.getChartpanel().isVisible()) {
			resPanel.getChartpanel().setVisible(true);
		}
		
		String resTxt = "";
		
		if (percent <= 40) {
			resTxt = "(It is a bad client)";
		}
		else if (percent > 40 && percent <= 50) {
			resTxt = "(It is a potential bad client)";
		}
		else if (percent > 50 && percent < 60) {
			resTxt = "(It is a potential good client)";
		}
		else {
			resTxt = "(It is a good client)";
		}
		
		resPanel.getMeterplot().setUnits("% "+ resTxt);
		
		resPanel.getMeterplot().setDataset(ds);
		resPanel.repaint();	
	}
	

	public JPanel getMainPanel() {
		return mainPanel;
	}

	public FormPanel getFormPanel() {
		return formPanel;
	}

	public ResultPanel getResPanel() {
		return resPanel;
	}
	
	
}

